﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRBIZE
{
    public partial class FMI_TC_TAKEOUT : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        private string sSite_No;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;
        private int iGridView2 = 0;


        public FMI_TC_TAKEOUT()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string sDate1 = uiDate1.Text;
            string sDate2 = uiDate2.Text;
            string SQL = "select c.fdictname,b.fstockname,d.username,a.* "
               +" from i_tc_takeout_head a"
               + " left join i_ts_stock b on a.fstockno = b.fstockno"
               + " left join s_ts_dict c on c.fdicttype = 2007 and c.fdictno = a.fbillstate"
               + " left join am_sys_user d on d.id = a.ftakeuser"
               + " where 1 = 1"
               + "  and a.ftakedate between '" + sDate1 + "'"
               + "  and '" + sDate2 + "'";
            DataRowView dr1 = (DataRowView)cbState.SelectedItem;
            if ((dr1 != null) && (cbState.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fbillstate=" + dr1.Row["fdictno"].ToString();
            }

            DataRowView dr2 = (DataRowView)cbfstockno.SelectedItem;
            if ((dr2 != null) && (cbfstockno.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fstockno='" + dr2.Row["fstockno"].ToString() + "'";
            }

            DataRowView dr3 = (DataRowView)cbftakeuser.SelectedItem;
            if ((dr3 != null) && (cbftakeuser.SelectedIndex > 0))
            {
                SQL = SQL + " and a.ftakeuser='" + dr3.Row["id"].ToString() + "'";
            }

            if (qifbillno.Text != "")
            {
                SQL = SQL + " and a.fbillno like '%" + qifbillno.Text + "%'";
            }

            SQL = SQL + "  order by a.ftakedate desc,a.fbillno desc";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        //执行打印代码
        protected override void OnSys07()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sPrintKey = "'" + uiDGView["fBillNo", rowindex].Value.ToString() + "'";

            base.OnSys07();
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            uifbillno.Text = uiDGView["fbillno", rowindex].Value.ToString();
            uiftakedate.Text = uiDGView["ftakedate", rowindex].Value.ToString();
            uifexplanation.Text = uiDGView["fexplanation", rowindex].Value.ToString();

            int i = 0;
            string sstatus = uiDGView["fbillstate", rowindex].Value.ToString();
            uifbillstate.SelectedValue = sstatus;
            foreach (System.Data.DataRowView dr in uifbillstate.Items)
            {
                string id = dr["fDictNo"].ToString();
                if (id.Equals(sstatus))
                {
                    uifbillstate.SelectedIndex = i;
                    break;
                }
                i = i + 1;
            }

            i = 0;
            string srole_id = uiDGView["fstockno", rowindex].Value.ToString();
            foreach (System.Data.DataRowView dr in uifstockno.Items)
            {
                string id = dr["fstockno"].ToString();
                if (id.Equals(srole_id))
                {
                    uifstockno.SelectedIndex = i;
                    break;
                }
                i = i + 1;
            }

            i = 0;
            string sftakeuser = uiDGView["ftakeuser", rowindex].Value.ToString();
            foreach (System.Data.DataRowView dr in uiftakeuser.Items)
            {
                string id = dr["id"].ToString();
                if (id.Equals(sftakeuser))
                {
                    uiftakeuser.SelectedIndex = i;
                    break;
                }
                i = i + 1;
            }

            sType = "V";
            uifbillno.ReadOnly = true;
            uifexplanation.ReadOnly = true;
            uiftakeuser.ReadOnly = true;
            uiftakedate.ReadOnly = true;
            uifbillstate.ReadOnly = true;
            uifstockno.ReadOnly = true;
            uiftakeuser.ReadOnly = true;

            //载入明细
            string sql = "select c.fequipmenttag fboxname,d.fequipmenttag fcabname,fline"
              + ", frfidbarcode,ISNULL(e.fpackname,a.fpackno) fpack,a.fgoodno,fgoodname,b.fspec,fqty,foutqty,fprocdate,fmemo"
              + ",fbillno,a.fbuyprice,ftotamt,fboxbarcode,fcabbarcode,a.foperator,a.fupddate"
              + " from i_tc_takeout_detail a"
              + " left join i_ts_good_info b on a.fgoodno = b.fgoodno"
              + " left join i_ts_warehouse c on c.fequipmentbarcode = a.fboxbarcode"
              + " left join i_ts_cabinet d on d.fequipmentbarcode = a.fcabbarcode"
              + " left join i_ts_pack e on e.fpackno=a.fpackno"
              + " where 1 = 1"
              + " and a.fbillno='" + uifbillno.Text + "'"
              + " order by fline";
            DataTable myDetail = StaticClass.myDB.query(sql);
            uiDetail.DataSource = myDetail;

            uiDetail.ReadOnly = true;
            base.OnSys10();

            //表格列显示统一控制
            if (iGridView2 == 0)
            {
                myCache.doSetGridVIew(sMenu + "1", sUser, uiDetail);
                iGridView2 = iGridView2 + 1;
            }
            
        }

        private void FMI_TC_TAKEOUT_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "Import_i_tc_takeout";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys02.Visible = false; sys02.Tag = "1";
            sys03.Visible = true;
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = true;

            uiDate1.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uiDate2.Text = DateTime.Now.ToString("yyyy-MM-dd");

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=2007");
            uifbillstate.DataSource = myTs_Dict;
            uifbillstate.DisplayMember = "fDictName";
            uifbillstate.ValueMember = "fDictNo";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fDictName"] = "请选择";
            dr1["fDictNo"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            cbState.DataSource = myTs_Dict;
            cbState.DisplayMember = "fDictName";
            cbState.ValueMember = "fDictNo";
            cbState.SelectedIndex = 0;

            DataTable myUser = myCache.doGetDict("AM_SYS_USER", "");
            uiftakeuser.DataSource = myUser;
            uiftakeuser.DisplayMember = "username";
            uiftakeuser.ValueMember = "id";

            DataRow dr2 = myUser.NewRow();
            dr2["username"] = "请选择";
            dr2["id"] = "0";
            myUser.Rows.InsertAt(dr2, 0);
            cbftakeuser.DataSource = myUser;
            cbftakeuser.DisplayMember = "username";
            cbftakeuser.ValueMember = "id";
            cbftakeuser.SelectedIndex = 0;

            DataTable myStock = myCache.doGetDict("I_TS_STOCK", "");
            uifstockno.DataSource = myStock;
            uifstockno.DisplayMember = "fstockname";
            uifstockno.ValueMember = "fstockno";

            DataRow dr3 = myStock.NewRow();
            dr3["fstockname"] = "请选择";
            dr3["fstockno"] = "0";
            myStock.Rows.InsertAt(dr3, 0);
            cbfstockno.DataSource = myStock;
            cbfstockno.DisplayMember = "fstockname";
            cbfstockno.ValueMember = "fstockno";
            cbfstockno.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
            //设置不自动生成列，此属性在属性面板中没有
            //dataGridView1.AutoGenerateColumns = false;
            //取消加载默认选中第一行
            //dataGridView1.ClearSelection();
            //自动适应列宽
            //dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

            //取出选中的单元格的值：
            //dataGridView1.SelectedCells中放着全部选中的单元格
            //if (dataGridView1.SelectedCells.Count > 0)
            //    {
            //        MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
            //    }
            //取出选中的行内容：
            //if (dataGridView1.SelectedRows.Count > 0)
            //    {
            //        MessageBox.Show(dataGridView1.SelectedRows[0].Cells[0].ToString());
            //    }
            //获取用于填充行绑定的对象：
            ////行对象使用属性：DataBoundItem
            //student sss = dataGridView1.SelectedRows[0].DataBoundItem as student;
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            int sfbillstate = int.Parse(uiDGView["fbillstate", rowindex].Value.ToString());

            if (sfbillstate > 1)
            {
                UIMessageTip.ShowOk("该单据不能删除");
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fbillno", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fbillno", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'i_tc_takeout_head','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //执行审核代码
        protected override void OnSys11()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            int sfbillstate = int.Parse(uiDGView["fbillstate", rowindex].Value.ToString());

            if (sfbillstate > 1)
            {
                UIMessageTip.ShowOk("该单据不能审核");
                return;
            }

            string sBillNo = uiDGView["fBillNo", rowindex].Value.ToString();

            if (this.ShowAskDialog("确定审核单据(" + sBillNo + ")?"))
            {
                string SQL = "exec proc_biz_bizcenter 'i_tc_takeout','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);
                this.ShowInfoDialog(myTable.Rows[0][0].ToString());

                OnSys04();
            }
        }

        private void uiDetail_DoubleClick(object sender, EventArgs e)
        {
            string iMenu = sMenu+"1";
            OnGridDbClick(iMenu, uiDetail);
        }

        private void cbState_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbfstockno_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbftakeuser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
